﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class user_user_func : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnInsert.Attributes.Add("OnClick", "javascript:return chk();");
            btnUpdate.Attributes.Add("OnClick", "javascript:return chk();");
            
            Get_dlTable();
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        /*
        if (txtcode.Text == "")
        {
            msg.Text = "必填";
            return;
        }
        */
        string sSql = "select count(*) from t_user_grantgroup where grpcode='" + txtcode.Text.Trim() + "'";
        TUserFunc oFunc = new TUserFunc();
        if ((!oFunc.FindKey("funccode", txtcode.Text.Trim()))&&(TSqlDB.ExecuteScalar(sSql)==0))
        {
            oFunc.FuncCode = txtcode.Text.Trim().ToUpper();
            oFunc.FuncName = txtname.Text.Trim();
            oFunc.SortNum = Convert.ToDecimal(txtsort.Text);
            if (isshow.Checked) { oFunc.Status = 0; } else { oFunc.Status = 1; };
            oFunc.Insert();
            //更新列表
            msg.Text = "*";
            Get_dlTable();
        }
        else
        {
            msg.Text = "重复";
        }
    }

    private void Get_dlTable()
    {
        string sSql = "select * from t_user_func order by sortnum";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        //关闭
        myReader.Free();
    }
    protected void dlTable_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton b = (LinkButton)e.CommandSource;
        switch (b.CommandName)
        {
            case "Modify":
                dlTable.SelectedIndex = e.Item.ItemIndex;
                funccode.Text = b.CommandArgument;
                TUserFunc oFunc = new TUserFunc(funccode.Text);
                oFunc.Select();
                txtcode.Text = oFunc.FuncCode;
                txtname.Text = oFunc.FuncName;
                txtsort.Text = oFunc.SortNum.ToString();
                if (oFunc.Status == 0)
                {
                    isshow.Checked = true;
                }
                else
                {
                    isshow.Checked = false;
                }
                msg.Text = "只读";
                txtcode.ReadOnly = true;
                btnInsert.Enabled = false;
                btnUpdate.Enabled = true;
                btnCancel.Enabled = true;
                break;
            case "Delete":
                TUserFunc.DeleteByID(b.CommandArgument);
                Get_dlTable();
                txtcode.Text = "";
                txtname.Text = "";
                msg.Text = "*";
                txtcode.ReadOnly = false;
                btnInsert.Enabled = true;
                btnUpdate.Enabled = false;
                btnCancel.Enabled = false;
                dlTable.SelectedIndex = -1;
                break;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        TUserFunc oFunc = new TUserFunc(funccode.Text);
        oFunc.FuncName = txtname.Text.Trim();
        oFunc.SortNum = Convert.ToDecimal(txtsort.Text);
        if (isshow.Checked) { oFunc.Status = 0; } else { oFunc.Status = 1; };
        oFunc.Update();

        txtcode.Text = "";
        txtname.Text = "";
        msg.Text = "*";
        txtcode.ReadOnly = false;
        btnInsert.Enabled = true;
        btnUpdate.Enabled = false;
        btnCancel.Enabled = false;
        dlTable.SelectedIndex = -1;
        
        Get_dlTable();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtcode.Text = "";
        txtname.Text = "";
        msg.Text = "*";
        txtcode.ReadOnly = false;
        btnInsert.Enabled = true;
        btnUpdate.Enabled = false;
        btnCancel.Enabled = false;
        dlTable.SelectedIndex = -1;
    }
}
